package com.ronghui.cash.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * 
 * @author huangyong
 * @since 1.0.0
 */
public final class DateUtil {

	private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat YYYYMMDDH24MISSFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	private DateUtil() {
	}

	/**
	 * 获取当前日期与时间
	 */
	public static String getCurrentDateTime() {
		return dateTimeFormat.format(new Date());
	}

	/**
	 * 获取当前日期 eg:2008-08-08
	 */
	public static String getCurrentDate() {
		return dateFormat.format(new Date());
	}

	/**
	 * YYYYMMDDH24MISS eg:20080808080808 即:2008-08-08 08:08:08
	 */
	public static String getYYYYMMDDH24MISSFormat() {
		return YYYYMMDDH24MISSFormat.format(new Date());
	}

	/*
	 * public static Date string2Date(String dateStr) throws ParseException{
	 * java.text.SimpleDateFormat formatter = new
	 * SimpleDateFormat("yyyy-MM-dd"); Date date = formatter.parse(dateStr);
	 * return date; }
	 */

	/**
	 * compareDate,if dateStr before today,return ture.
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static boolean compareDate(String dateStr) throws ParseException {
		Date nowdate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(dateStr);
		return d.before(nowdate);

		/*
		 * boolean flag = d.before(nowdate); if(flag) System.out.print("早于今天");
		 * else System.out.print("晚于今天");
		 */
	}

	/**
	 * 比较两个日期的大小
	 * @param DATE1
	 * @param DATE2
	 * @return
	 */
	public static int compare_date(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				//System.out.println("dt1 在dt2前");
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				//System.out.println("dt1在dt2后");
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 转yyyy-MM-dd为MM月dd日
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static String convertFormat(String dateStr) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("MM月dd日");
		Date d = sdf.parse(dateStr);

		return sdf2.format(d);

		/*
		 * boolean flag = d.before(nowdate); if(flag) System.out.print("早于今天");
		 * else System.out.print("晚于今天");
		 */
	}

	/**
	 * yyyy年MM月dd日
	 *
	 * @return
	 * @throws ParseException
	 */
	public static String getCurrentDateYMD() {
		return new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
	}

	public static int clacDate(String bdate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		long time1 = cal.getTimeInMillis();
		try {
			cal.setTime(sdf.parse(bdate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time2 = cal.getTimeInMillis();
		int days = (int) ((time2 - time1) / (1000 * 3600 * 24));
		return days;
	}

	/**
	 * 指定日期与当前日期相差的月数
	 *
	 * @param oldDate
	 *            旧的日期
	 * @return 如果d1>d2返回 月数差 否则返回0
	 * @throws ParseException
	 */
	public static int getMonthDiff(String oldDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		try {
			c2.setTime(sdf.parse(oldDate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (c1.getTimeInMillis() < c2.getTimeInMillis())
			return 0;
		int year1 = c1.get(Calendar.YEAR);
		int year2 = c2.get(Calendar.YEAR);
		int month1 = c1.get(Calendar.MONTH);
		int month2 = c2.get(Calendar.MONTH);
		int day1 = c1.get(Calendar.DAY_OF_MONTH);
		int day2 = c2.get(Calendar.DAY_OF_MONTH);
		// 获取年的差值 假设 d1 = 2015-8-16 d2 = 2011-9-30
		int yearInterval = year1 - year2;
		// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
		if (month1 < month2 || month1 == month2 && day1 < day2)
			yearInterval--;
		// 获取月数差值
		int monthInterval = (month1 + 12) - month2;
		if (day1 < day2)
			monthInterval--;
		monthInterval %= 12;
		return yearInterval * 12 + monthInterval;
	}

	/**
	 * 获得几天前/几天后的日期
	 * 
	 * @param p
	 *              天数 正数为p天后，负数为p天前  
	 */
	public static String getCaleDateString(int p) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, p);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(calendar.getTime());
	}
	
	public static String secToTime(int time) {
	// a integer to xx:xx:xx
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10) {
			retStr = "0" + Integer.toString(i);
		}else {
			retStr = "" + i;
		}
		return retStr;
	}
	
	/**
	 * 获取当前时间的 hours 之前或之后的日期与时间
	 * @param hours 负数为获取hours小时之前的时间，正数为获取hours小时之后的时间
	 * @return
	 */
	public static String headDateTime(int hours) throws ParseException {
	    Calendar cal = Calendar.getInstance();
	    cal.add(Calendar.HOUR, hours);
	    return dateTimeFormat.format(cal.getTime());
	}


	/**
	 * 获取当月的第一天
	 * @return
	 */
	public static String getMonthFirstDay(){
		Calendar cal=Calendar.getInstance();//获取当前日期
		cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
		return dateFormat.format(cal.getTime());
	}

	/**
	 * 获取当月的最后一天
	 * @return
	 */
	public static String getMonthLastDay(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(cal.getTime());
	}

	/**
 　　* 判断时间是否在时间段内 *
 　　* @param time
 　　* 当前时间 HH:mm
 　　* @param strDateBegin
 　　* 开始时间 00:00
 　　* @param strDateEnd
 　　* 结束时间 05:00
 　　* @return
 　　*/
   public static boolean isInTimeBucket(String strDateBegin, String strDateEnd) {
	   String type = "HH:mm";
	   String nowTime = new SimpleDateFormat(type).format(new Date());

	   try {
		   if(DateCompare(nowTime,strDateBegin,type) >= 0 && DateCompare(nowTime,strDateEnd,type)<=0){
               return true;
           }
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	   return false;
	   /*
	   //例如比较当前时间和早上6：00
	   String nowTime = new SimpleDateFormat("HH:MM").format(new Date());
	   System.out.println("当前时间为："+nowTime);
	   System.out.println("与当日09：00相比");
	   int i = DateCompare(nowTime,"09:00","HH:MM");
	   switch (i) {
		   case 0:
			   System.out.println("两个时间相等");
			   break;
		   case 1:
			   System.out.println("当前时间晚于06:00");
			   break;
		   case -1:
			   System.out.println("当前时间早于06:00");
			   break;
		   default:
			   break;
	   }*/
   }
	/**
	 * 根据时间类型比较时间大小
	 *
	 * @param source
	 * @param traget
	 * @param type "YYYY-MM-DD" "yyyyMMdd HH:mm:ss"  类型可自定义
	 * @return
	 *  0 ：source和traget时间相同
	 *  1 ：source比traget时间大
	 *  -1：source比traget时间小
	 * @throws Exception
	 */
	public static int DateCompare(String source, String traget, String type) throws Exception {
		int ret = 2;
		SimpleDateFormat format = new SimpleDateFormat(type);
		Date sourcedate = format.parse(source);
		Date tragetdate = format.parse(traget);
		ret = sourcedate.compareTo(tragetdate);
		return ret;
	}
}
